package com.setup.trac.service;

import java.security.GeneralSecurityException;
import java.util.Iterator;
import java.util.Set;

import com.setup.trac.dao.DAOService;
import com.setup.trac.pojo.Progetti;
import com.setup.trac.pojo.Utenti;
import com.setup.trac.util.TracSetupUtil;

public class AdminService {

	private DAOService daoService;
	private TracSetupUtil tracSetupUtil;

	public Utenti insertUtente(Utenti u) throws GeneralSecurityException {
		try {
			u.setPassword(getTracSetupUtil().md5(
					u.getUsername() + u.getPassword()));
			u.setPrimoAccesso(true);
			getDaoService().insert(u);
			Set<Progetti> progettis = u.getProgettis();
			if (progettis.size() > 0) {
				for (Iterator iterator = progettis.iterator(); iterator
						.hasNext();) {
					Progetti p = (Progetti) iterator.next();
				}
			}
			return u;
		} catch (GeneralSecurityException e) {
			throw e;
		}
	}

	public DAOService getDaoService() {
		return daoService;
	}

	public void setDaoService(DAOService daoService) {
		this.daoService = daoService;
	}

	public TracSetupUtil getTracSetupUtil() {
		return tracSetupUtil;
	}

	public void setTracSetupUtil(TracSetupUtil tracSetupUtil) {
		this.tracSetupUtil = tracSetupUtil;
	}

	public void updateUser(Utenti user) {
		daoService.update(user);
	}
}
